package com.yyfax.ph.progress.checkCredit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.widget.GridView;
import butterknife.BindView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.ph.R;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.common.utils.Constants;

import java.util.HashMap;
import java.util.List;

public class CheckCreditActivity extends BaseYYFaxActivity implements CheckCreditPresenter.View {

	private static final String EXTRA_RCMD_ID = "570";
	private static final String EXTRA_CREDIT_TYPE = "571";
	private static final int LOADER_ID = 1966;
	@BindView(R.id.gv)
	GridView mGv;
	private CheckCreditPresenter mPresenter;

	public static void start(Activity aty, String rcmdId, String creditType) {
		Intent intent = new Intent(aty, CheckCreditActivity.class);
		intent.putExtra(EXTRA_RCMD_ID, rcmdId);
		intent.putExtra(EXTRA_CREDIT_TYPE, creditType);
		aty.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setBtnBackVisible(true);
		setupTopBar(R.string.rcm_details, true, false, true, false);
		setTitle(R.string.check_credit_report);
	}

	@Override
	public int getContentViewRes() {
		return R.layout.activity_check_credit;
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		if (mPresenter == null) {
			mPresenter = (CheckCreditPresenter) data;
			mPresenter.attach(this);
			mPresenter.getDetailCredit(getIntent().getStringExtra(EXTRA_RCMD_ID));
		}
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new CheckCreditPresenter.Generator());
	}

	@Override
	public void onStop() {
		super.onStop();
		getSupportLoaderManager().destroyLoader(LOADER_ID);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	public void onGetCheckCreditSuccess(List<CheckCreditModel.Credit> credit) {
		String type = getIntent().getStringExtra(EXTRA_CREDIT_TYPE);
		HashMap<String, String> option = null;
		if (TextUtils.equals(type, Constants.NO_CHANNEL_CREDIT)) {
			option = getOption();
		}
		mGv.setAdapter(new CreditImageAdapter(credit, option));
	}

	@Override
	public void onGetCheckCreditFailure(String tips) {
	}

	protected HashMap<String, String> getOption() {
		HashMap<String, String> map = new HashMap<>();
		map.put(Constants.IMAGE_TYPE_CREDIT, getString(R.string.credit_report));
		map.put(Constants.IMAGE_TYPE_PORTRAIT, getString(R.string.handle_id_card));
		map.put(Constants.IMAGE_TYPE_ID_CARD_FRONT, getString(R.string.id_front));
		map.put(Constants.IMAGE_TYPE_ID_CARD_BACK, getString(R.string.id_back));
		map.put(Constants.IMAGE_TYPE_QUERY_AUTHORIZE, getString(R.string.credit_query_auth_paper));
		map.put(Constants.IMAGE_TYPE_QUERY_APPLY, getString(R.string.credit_consult_auth_paper));
		return map;
	}
}
